home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.03 Mar 97 / Getting Started, ShapeWorld / ShapeWorld.java < prev   
Encoding:
Java Source  |  1997-01-21  |  4.9 KB  |  259 lines  |  [TEXT/CWIE]

  1. import java.awt.*;
  2. import java.util.*;
  3.  
  4. abstract class Shape
  5. {
  6.     boolean        highlighted;
  7.     ShapeCanvas    shapeCanvas;
  8.     int            shapeX, shapeY, shapeWidth, shapeHeight;
  9.     Rectangle    boundsRect;
  10.     
  11.     Shape( ShapeCanvas canv, int x, int y, int width, int height )
  12.     {
  13.         shapeCanvas = canv;
  14.         highlighted = false;
  15.         
  16.         shapeX = x;
  17.         shapeY = y;
  18.         shapeWidth = width;
  19.         shapeHeight = height;
  20.         
  21.         boundsRect = new Rectangle( x, y, width, height );
  22.     }
  23.     
  24.     abstract public void draw( Graphics g );
  25.     
  26.     public void erase( Graphics g ) //Added for Step2
  27.     {
  28.         g.clearRect( shapeX, shapeY, shapeWidth, shapeHeight );
  29.     }
  30.     
  31.     public void setHighlight( boolean newHighlight )
  32.     {
  33.         highlighted = newHighlight;
  34.     }
  35.     
  36.     public boolean isHighlighted()
  37.     {
  38.         return highlighted;
  39.     }
  40.     
  41.     public boolean isPointInShape( int x, int y )
  42.     {
  43.         return boundsRect.inside( x, y );
  44.     }
  45.  
  46.     public void    moveThisMuch( int x, int y ) //Added for Step2
  47.     {
  48.         boundsRect.move( shapeX + x, shapeY + y );
  49.         shapeX = boundsRect.x;
  50.         shapeY = boundsRect.y;
  51.         shapeWidth = boundsRect.width;
  52.         shapeHeight = boundsRect.height;
  53.     }
  54. }
  55.  
  56. class RectShape extends Shape
  57. {
  58.     RectShape( ShapeCanvas canv, int x, int y, int width, int height )
  59.     {
  60.         super( canv, x, y, width, height );
  61.     }
  62.     
  63.     public void draw( Graphics g )
  64.     {
  65.         if ( isHighlighted() )
  66.         {
  67.             g.setColor( Color.black );
  68.             g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
  69.             g.setColor( Color.red );
  70.             g.fillRect( shapeX+2, shapeY+2, shapeWidth-4, shapeHeight-4 );
  71.         }
  72.         else
  73.         {
  74.             g.setColor( Color.red );
  75.             g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
  76.         }
  77.     }
  78. }
  79.  
  80. //Step 1: Add new Shape subclass
  81. class OvalShape extends Shape
  82. {
  83.     OvalShape( ShapeCanvas canv, int x, int y, int width, int height )
  84.     {
  85.         super( canv, x, y, width, height );
  86.     }
  87.     
  88.     public void draw( Graphics g )
  89.     {
  90.         if ( isHighlighted() )
  91.         {
  92.             g.setColor( Color.black );
  93.             g.fillOval( shapeX, shapeY, shapeWidth, shapeHeight );
  94.             g.setColor( Color.red );
  95.             g.fillOval( shapeX+2, shapeY+2, shapeWidth-4, shapeHeight-4 );
  96.         }
  97.         else
  98.         {
  99.             g.setColor( Color.red );
  100.             g.fillOval( shapeX, shapeY, shapeWidth, shapeHeight );
  101.         }
  102.     }
  103. }
  104. //END Step 1
  105.  
  106. class ShapeCanvas extends Canvas
  107. {
  108.     Vector        shapes;
  109.     Shape        curShape;
  110.     Point        mousePosition; //Part of Step 2, used for drag tracking
  111.     
  112.     ShapeCanvas( int width, int height )
  113.     {
  114.         shapes = new Vector();
  115.         curShape = null;
  116.         
  117.         setBackground( Color.yellow );
  118.         
  119.         resize( width, height );
  120.     }
  121.     
  122.     public void addShape( Shape newShape )
  123.     {
  124.         shapes.addElement( newShape );
  125.     }
  126.     
  127.     public void paint( Graphics g )
  128.     {
  129.         for ( Enumeration e = shapes.elements(); e.hasMoreElements(); )
  130.         {
  131.             Shape s = (Shape)e.nextElement();
  132.             s.draw( g );
  133.         }
  134.     }
  135.     
  136.     public Shape findInShapeList( int x, int y )
  137.     {
  138.         for ( Enumeration e = shapes.elements(); e.hasMoreElements(); )
  139.         {
  140.             Shape s = (Shape)e.nextElement();
  141.             
  142.             if ( s.isPointInShape( x, y ) )
  143.             {
  144.                 s.draw( getGraphics() );
  145.                 
  146.                 return s;
  147.             }
  148.         }
  149.         
  150.         return null;
  151.     }
  152.     
  153.     public void update (Graphics g)
  154.     {
  155.             paint(g);
  156.     }
  157.     
  158.     public boolean mouseDown( Event e, int x, int y )
  159.     {
  160.         curShape = findInShapeList( x, y );
  161.         
  162.         if ( curShape != null ) //For Step2
  163.         {
  164.             curShape.setHighlight( true );
  165.             mousePosition = new Point( x, y );
  166.             
  167.             return true;
  168.         }
  169.             
  170.         return false;
  171.     }
  172.     
  173. //Step 2: Add Shape dragging
  174.     public boolean mouseDrag( Event e, int x, int y )
  175.     {
  176.         Graphics    g;
  177.         
  178.         if ( curShape != null )
  179.         {
  180.             g = getGraphics();
  181.             
  182.             curShape.erase( g );
  183.             
  184.             curShape.moveThisMuch( x - mousePosition.x,
  185.                 y - mousePosition.y );
  186.                 
  187.             mousePosition.x = x;
  188.             mousePosition.y = y;
  189.             
  190.             curShape.draw( g );
  191.             
  192.             return true;
  193.         }
  194.         
  195.         return false;
  196.     }
  197.     
  198.     public boolean mouseUp( Event e, int x, int y )
  199.     {
  200.         if ( curShape != null )
  201.         {
  202.             curShape.setHighlight( false );
  203.             repaint();
  204.             
  205.             return true;    
  206.         }
  207.         
  208.         return false;
  209.     }
  210. //End Step 2
  211. }
  212.  
  213. public class ShapeWorld extends java.applet.Applet
  214. {
  215.     ShapeCanvas    sCanvas;
  216.     final int    shapeWidth = 20;
  217.     final int    shapeHeight = 20;
  218.     
  219.     public void init()
  220.     {
  221.         int            x, y;
  222.         
  223.         sCanvas = new ShapeCanvas( 440, 290 );
  224.         add( sCanvas );
  225.         
  226.         Random ran = new Random();
  227.         Rectangle b = sCanvas.bounds();
  228.         
  229.         for ( int i=1; i<=10; i++ )
  230.         {
  231.             x = b.x + (int)((float)(b.width) * ran.nextFloat() );
  232.             if ( x > b.x + b.width - shapeWidth )
  233.                 x -= shapeWidth;
  234.                 
  235.             y = b.y + (int)((float)(b.height) * ran.nextFloat() );
  236.             if ( y > b.y + b.height - shapeHeight )
  237.                 y -= shapeHeight;
  238.             
  239.             RectShape r = new RectShape( sCanvas, x, y, shapeWidth, shapeHeight );
  240.             sCanvas.addShape( r );
  241.         }
  242.  
  243. //Step3: Create some of the new shape        
  244.         for ( int i=1; i<=10; i++ )
  245.         {
  246.             x = b.x + (int)((float)(b.width) * ran.nextFloat() );
  247.             if ( x > b.x + b.width - shapeWidth )
  248.                 x -= shapeWidth;
  249.                 
  250.             y = b.y + (int)((float)(b.height) * ran.nextFloat() );
  251.             if ( y > b.y + b.height - shapeHeight )
  252.                 y -= shapeHeight;
  253.             
  254.             OvalShape r = new OvalShape( sCanvas, x, y, shapeWidth, shapeHeight );
  255.             sCanvas.addShape( r );
  256.         }
  257. //End Step3
  258.     }
  259. }